package net.wsfcp.android.ui.view.main.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import net.wsfcp.android.R;
import net.wsfcp.android.adapter.DrawerAdapter;
import net.wsfcp.android.base.BaseFragment;
import net.wsfcp.android.bean.resultbean.DrawerBean;
import net.wsfcp.android.event.ReloadPageEvent;
import net.wsfcp.android.http.mvp.contract.DrawerContract;
import net.wsfcp.android.http.mvp.presenter.DrawerPresenter;
import net.wsfcp.android.ui.view.main.activity.WebViewActivity;
import net.wsfcp.android.ui.widget.RecycleViewDivider;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

//首页侧滑菜单
public class DrawerFragment extends BaseFragment<DrawerPresenter> implements DrawerContract.DrawerView {
    @BindView(R.id.rv_drawer)
    RecyclerView rvDrawer;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_content)
    TextView txtContent;


    DrawerAdapter drawerAdapter;
    List<DrawerBean.ChannelList> drawerBeans = new ArrayList<DrawerBean.ChannelList>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_drawer;
    }


    @Override
    protected DrawerPresenter loadPresenter() {
        return new DrawerPresenter();
    }

    @Override
    protected void initData() {
        mPresenter.getDrawer();
    }

    @Override
    protected void initListener() {
        drawerAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Intent intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("url", drawerBeans.get(position).getPageLink());
                intent.putExtra("mTitle", drawerBeans.get(position).getName());

                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected void initView() {
        initRecyclerView();
        initAdapter();

    }


    public void initAdapter() {
        drawerAdapter = new DrawerAdapter(getContext(), R.layout.item_list_drawer, drawerBeans);
        rvDrawer.setAdapter(drawerAdapter);
    }


    public void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
//设置布局管理器
        rvDrawer.setLayoutManager(layoutManager);
//设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
//设置Adapter

        //设置分隔线
        rvDrawer.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayoutManager.VERTICAL, dp(8), getResources().getColor(R.color.theme_grey)));

////设置增加或删除条目的动画
//        rvDrawer.setItemAnimator( new DefaultItemAnimator());

    }


    @Override
    public void getDrawerSuccess(DrawerBean drawerBean) {
        Glide.with(getContext()).load(drawerBean.getModuleSlideMenuData().getFilePath()).into(img);
        txtTitle.setText(drawerBean.getModuleSlideMenuData().getTitle());
        txtContent.setText(drawerBean.getModuleSlideMenuData().getContent());

        drawerBeans.clear();
        drawerBeans.addAll(drawerBean.getChannelList());
        drawerAdapter.notifyDataSetChanged();


    }

    @Override
    public void getDrawerFail(String failMsg) {
//        toast(failMsg);

    }


    @Subscribe(threadMode = ThreadMode.POSTING)
    public void login(ReloadPageEvent event) {
        if (drawerBeans.size() == 0)
            initData();
//        switch (event.getCode()) {
//            case 0:   // 退出登录
//                finish();
//                break;
//            case 1:   // 登录
//                finish();
//                break;
//            case 2:   // 忘记密码
//                finish();
//                break;
//
//        }

    }

}
